package com.fdb.efp.nls.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.efp.nls.service.dao.SRoleRescRuleDao;
import com.fdb.efp.nls.service.domain.SRoleRescRule;
import com.fdb.efp.nls.service.facade.SRoleRescRuleService;
import com.fdb.efp.nls.service.vo.SRoleRescRuleParamVO;
import com.fdb.efp.nls.service.vo.SRoleRescRuleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * ServiceImpl: 系统角色对应资源操作信息表
 * <p>
 * 
 * @author fuzm
 * @date 2018-05-04
 */
@Service("sRoleRescRuleService")
public class SRoleRescRuleServiceImpl implements SRoleRescRuleService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SRoleRescRuleServiceImpl.class);

	@Autowired
	private SRoleRescRuleDao sRoleRescRuleDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertSRoleRescRule(SRoleRescRuleVO inSRoleRescRuleVo) {
		logger.debug("当前新增数据为:" + inSRoleRescRuleVo.toString());
		int num = 0;
		try {
			SRoleRescRule sRoleRescRule = new SRoleRescRule();
			beanCopy(inSRoleRescRuleVo, sRoleRescRule);
			num = sRoleRescRuleDao.insertSRoleRescRule(sRoleRescRule);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(SRoleRescRuleVO inSRoleRescRuleVo) {
		logger.debug("当前删除数据条件为:" + inSRoleRescRuleVo);
		int num = 0;
		try {
			SRoleRescRule sRoleRescRule = new SRoleRescRule();
			beanCopy(inSRoleRescRuleVo, sRoleRescRule);
			num = sRoleRescRuleDao.deleteByPk(sRoleRescRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRoleRescRuleVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(SRoleRescRuleVO inSRoleRescRuleVo) {
		logger.debug("当前修改数据为:" + inSRoleRescRuleVo.toString());
		int num = 0;
		try {
			SRoleRescRule sRoleRescRule = new SRoleRescRule();
			beanCopy(inSRoleRescRuleVo, sRoleRescRule);
			num = sRoleRescRuleDao.updateByPk(sRoleRescRule);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRoleRescRuleVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public SRoleRescRuleVO queryByPk(SRoleRescRuleVO inSRoleRescRuleVo) {

		logger.debug("当前查询参数信息为:" + inSRoleRescRuleVo);
		try {
			SRoleRescRule querySRoleRescRule = new SRoleRescRule();
			beanCopy(inSRoleRescRuleVo, querySRoleRescRule);
			SRoleRescRule queryRslSRoleRescRule = sRoleRescRuleDao.queryByPk(querySRoleRescRule);
			if (Objects.nonNull(queryRslSRoleRescRule)) {
				SRoleRescRuleVO outSRoleRescRuleVo = beanCopy(queryRslSRoleRescRule, new SRoleRescRuleVO());
				logger.debug("当前查询结果为:" + outSRoleRescRuleVo.toString());
				return outSRoleRescRuleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleRescRuleVO> queryAllOwner(SRoleRescRuleVO sRoleRescRuleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRoleRescRule sRoleRescRule = beanCopy(sRoleRescRuleVo, new SRoleRescRule());
		List<SRoleRescRuleVO> list = null;
		try {
			List<SRoleRescRule> sRoleRescRules = sRoleRescRuleDao.queryAllOwnerByPage(sRoleRescRule);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sRoleRescRules.size());
			pageSet(sRoleRescRules, sRoleRescRule);
			list = (List<SRoleRescRuleVO>) beansCopy(sRoleRescRules, SRoleRescRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleRescRuleVO> queryAllCurrOrg(SRoleRescRuleVO sRoleRescRuleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRoleRescRule sRoleRescRule = beanCopy(sRoleRescRuleVo, new SRoleRescRule());
		List<SRoleRescRule> sRoleRescRules = sRoleRescRuleDao.queryAllCurrOrgByPage(sRoleRescRule);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sRoleRescRules.size());
		List<SRoleRescRuleVO> list = null;
		try {
			pageSet(sRoleRescRules, sRoleRescRule);
			list = (List<SRoleRescRuleVO>) beansCopy(sRoleRescRules, SRoleRescRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRoleRescRuleVO> queryAllCurrDownOrg(SRoleRescRuleVO sRoleRescRuleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRoleRescRule sRoleRescRule = beanCopy(sRoleRescRuleVo, new SRoleRescRule());
		List<SRoleRescRule> sRoleRescRules = sRoleRescRuleDao.queryAllCurrDownOrgByPage(sRoleRescRule);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sRoleRescRules.size());
		List<SRoleRescRuleVO> list = null;
		try {
			pageSet(sRoleRescRules, sRoleRescRule);
			list = (List<SRoleRescRuleVO>) beansCopy(sRoleRescRules, SRoleRescRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public List<String> queryRoleRescRules(SRoleRescRuleVO sRoleRescRuleVo) {
		logger.debug("查询角色对应的资源操作信息,roleCode:");
		SRoleRescRule sRoleRescRule = beanCopy(sRoleRescRuleVo, new SRoleRescRule());
		List<String> rescRescRules = sRoleRescRuleDao.queryRoleRescRules(sRoleRescRule);
		logger.debug("查询角色对应的资源操作信息的结果集数量为:" + rescRescRules.size());

		return rescRescRules;
	}

	@Override
	public int saveRoleRescRule(SRoleRescRuleParamVO sRoleRescRuleParamVO) {
		int flag = 0;
		String rescActDesc = "菜单访问";
		String roleCode = sRoleRescRuleParamVO.getRoleCode();
		String rescCode = sRoleRescRuleParamVO.getRescCode();
		String loginUserId = sRoleRescRuleParamVO.getLoginUserId();
		SRoleRescRule sRoleRescRule = new SRoleRescRule();
		sRoleRescRule.setRoleCode(roleCode);
		sRoleRescRule.setRescCode(rescCode);
		flag += sRoleRescRuleDao.deleteRoleRescRule(sRoleRescRule);
		String roleRescRuleStrs = sRoleRescRuleParamVO.getRoleRescRuleStrs();
		List<String> rescActCodes = null;
		if (!StringUtils.isEmpty(roleRescRuleStrs)) {
			String[] roleRescRuleArr = roleRescRuleStrs.split(",");
			rescActCodes = Arrays.asList(roleRescRuleArr);
		}

		if (null != rescActCodes && rescActCodes.size() > 0) {
			// 查找 当前资源码 上级及以上的 菜单访问 资源权限码,并且赋值给当前角色
			List<SRoleRescRule> roleRescRules = parentRescActCode(rescCode, rescActDesc, roleCode, loginUserId);
			for (String rescActCode : rescActCodes) {
				sRoleRescRule = new SRoleRescRule();
				sRoleRescRule.setRoleCode(roleCode);
				sRoleRescRule.setRescCode(rescCode);
				sRoleRescRule.setRescActCode(rescActCode);
				sRoleRescRule.setLoginUserId(loginUserId);

				roleRescRules.add(sRoleRescRule);
				// flag += sRoleRescRuleDao.addRoleRescRule(sRoleRescRule);
			}
			flag = sRoleRescRuleDao.insertMoreRoleRescRule(roleRescRules);
		}
		return flag;
	}

	/**
	 * 查询 资源码 上级及以上的 资源操作名的 资源权限码
	 * 
	 * @param rescCode
	 * @param rescActName
	 * @return
	 */
	private List<SRoleRescRule> parentRescActCode(String rescCode, String rescActDesc, String roleCode, String loginUserId) {
		List<SRoleRescRule> rescActs = new ArrayList<>();
		List<SRoleRescRuleVO> parents = sRoleRescRuleDao.queryParentRescInfo(rescCode, rescActDesc, roleCode);
		SRoleRescRule roleResc;
		SRoleRescRuleVO roleRescVO;
		while (null != parents && parents.size() > 0) {
			roleRescVO = parents.get(0);
			if (!StringUtil.isEmpty(roleRescVO.getRescActCode())) {
				roleResc = new SRoleRescRule();
				roleResc.setRescCode(roleRescVO.getRescCode());
				roleResc.setRescActCode(roleRescVO.getRescActCode());
				roleResc.setLoginUserId(loginUserId);
				roleResc.setRoleCode(roleCode);
				rescActs.add(roleResc);
			}

			parents = sRoleRescRuleDao.queryParentRescInfo(roleRescVO.getRescCode(), rescActDesc, roleCode);
		}
		return rescActs;
	}
}
